﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AISservice.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AISservice.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DefaultController : ControllerBase
    {

        private static Dictionary<string, Pair<MethodInfo, object>> _Method_List;
        static Dictionary<string, Pair<MethodInfo, object>> Method_List
        {
            get
            {
                if (_Method_List == null)
                    _Method_List = new Dictionary<string, Pair<MethodInfo, object>>();
                return _Method_List;
            }
        }


        private static object ExecuteMethod(String controler, string action, params object[] param)
        {

            if (Method_List.ContainsKey(controler + "." + action))
            {

                var method = (Method_List[controler + "." + action].First as MethodInfo);
                //foreach (object obj in param)
                //{
                //    Console.WriteLine(DateTime.Now.ToLongTimeString() + "  Param:" + obj);
                //    System.Diagnostics.Debugger.Log(0, null, string.Format("队列{0}处理请求参数{1}", Properties.Settings.Default.MQQueue
                //                 , obj==null ?"":  obj.ToString()));
                //}
                return JsonHelper.ObjectToJson(method.Invoke(Method_List[controler + "." + action].Second, param));  //Newtonsoft.Json.JsonConvert.SerializeObject(method.Invoke(ObjCache[controler], param));
            }
            return "";


        }
        [HttpPost]
        public static string Invoke(MQRequest req)
        {
            string execresult = string.Empty;
            if (Method_List.ContainsKey(req.Controller + "." + req.Action))
            {
                object[] Objparams = DealParam(req.Controller, req.Action, req.MessageBody);    //JsonHelper.JsonToObject<object[]>(request.MessageBody);   //JsonConvert.DeserializeObject<Object[]>(request.MessageBody);
                execresult = ExecuteMethod(req.Controller, req.Action, Objparams).ToString();
            }
            return execresult;
        }

        private static object[] DealParam(String controler, string anction, string strparam)
        {
            //todo:这里建议不要在参数最外面增加多"，而是传入实体对象的数组
            List<object> objparam = new List<object>();
            Dictionary<string, object> pairs = null;
            if (anction.Equals("Update"))
            {
                pairs = new Dictionary<string, object>();
                List<string> vs = new List<string>();
                foreach (string entry in JsonHelper.JsonToObject<List<string>>(strparam))
                {
                    vs.Add(entry);
                }
                pairs.Add("objlist", vs.ToArray());
            }
            else if (anction.Equals("SaveView"))
            {
                pairs = new Dictionary<string, object>();
                List<string> vs = new List<string>();
                foreach (string entry in JsonHelper.JsonToObject<List<string>>(strparam))
                {
                    vs.Add(entry);
                }
                pairs.Add("objlist", vs.ToArray());
            }
            else
                pairs = JsonHelper.JsonToObject<Dictionary<string, object>>(strparam);

            MethodInfo method = (Method_List[controler + "." + anction].First as MethodInfo);
            if (method != null)
            {
                ParameterInfo[] parameters = method.GetParameters();
                foreach (ParameterInfo info in parameters)
                {
                    if (pairs.ContainsKey(info.Name))
                    {
                        Object value = pairs[info.Name];

                        if (info.ParameterType == typeof(Int32))
                        {
                            value = Convert.ToInt16(value);
                        }
                        objparam.Add(value);
                    }
                    else
                    {
                        objparam.Add(null);
                    }

                }
            }

            return objparam.ToArray();

        }
    }
}